package com.zs.java.blockingqueue;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * 不存储元素的阻塞队列
 *
 * @author madison
 * @description
 * @date 2021/6/1 23:03
 */
public class SynchronousQueueTest {
    public static void main(String[] args) throws InterruptedException {
        SynchronousQueue<Integer> queue = new SynchronousQueue();
        new Thread(() -> {
            try {
                System.out.println("等待获取数据......");
                TimeUnit.SECONDS.sleep(2);
                System.out.println("还没数据,继续等待...");
                System.out.println("获取到数据:"+queue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(()->{
            try {
                System.out.println("等以后存储数据...");
                TimeUnit.SECONDS.sleep(6);
                System.out.println("马上存储数据...");
                queue.put(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();


//        System.out.println(queue.take());
//        queue.put(4);
//        System.out.println(queue);
    }
}
